﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using Silverlight.ControlToolkit.Windows;

namespace Silverlight.ControlToolkit.Validator
{

    public enum ValidationSummaryDisplayMode
    {
        None,
        Simple,
        List,
        SingleParagraph
    }

    public class ValidationSummary
    {

        #region DisplayMode

        public static readonly DependencyProperty DisplayModeProperty =
                DependencyProperty.RegisterAttached("DisplayMode", typeof(ValidationSummaryDisplayMode), typeof(ValidationSummary),
                                                    new PropertyMetadata(ValidationSummaryDisplayMode.None));

        /// <summary>
        /// Gets the current DisplayMode associated with the control
        /// </summary>
        /// <param name="d">Dependecy Object / Control</param>
        /// <returns></returns>
        public static ValidationSummaryDisplayMode GetDisplayMode(DependencyObject d)
        {
            return (ValidationSummaryDisplayMode)d.GetValue(DisplayModeProperty);
        }

        /// <summary>
        /// Sets the Data Type property
        /// </summary>
        /// <param name="d">Dependecy Object / Control</param>
        /// <param name="value">Data Type to be set on the control</param>
        public static void SetDisplayMode(DependencyObject d, ValidationSummaryDisplayMode value)
        {
            d.SetValue(DisplayModeProperty, value);
        }


        #endregion


        public static void DisplayValidationSummary(DependencyObject containerControl, List<InvalidControl> invalidControls)
        {

            if (invalidControls.Count == 0) return;

            ValidationSummaryDisplayMode displayMode = GetDisplayMode(containerControl);

            switch (displayMode)
            {
                case ValidationSummaryDisplayMode.None:
                    break;

                case ValidationSummaryDisplayMode.Simple:
                    DisplaySimple(containerControl, invalidControls);
                    break;

                case ValidationSummaryDisplayMode.SingleParagraph:
                    //TODO:
                    break;

                case ValidationSummaryDisplayMode.List:
                    //TODO:
                    break;
            }

        }


        private static void DisplaySimple(DependencyObject containerControl, List<InvalidControl> invalidControls)
        {

            StringBuilder stbMessage = new StringBuilder();

            //if (objValidationSummaryProperties.ErrorMessage.Trim.Length > 0)
            //{
            //    stbMessage.Append(objValidationSummaryProperties.ErrorMessage + Environment.NewLine);
            //}

            foreach (InvalidControl currentInvalidControl in invalidControls)
            {
                stbMessage.Append(Environment.NewLine + "- " + currentInvalidControl.ErrorMessage);
            }

            //MessageBox.Show(stbMessage.ToString(), this.GetErrorCaption(containerControl, objValidationSummaryProperties), MessageBoxButtons.OK, MessageBoxIcon.Error);

            FrameworkElement owner = null;
            if (containerControl is FrameworkElement)
                owner = containerControl as FrameworkElement;

            MessageWindow.Show(owner, stbMessage.ToString(), "Error ");

            //if (invalidControls(0).Control.CanFocus) invalidControls(0).Control.Focus();

        }

    }
}
